package antidestiny.java2dex.ui.view;


import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import antidestiny.utilib.Aide;

/**
 * Created by Hero on 2017/1/2.
 */

/**
 * 宽度为父控件的宽度乘一个固定比例
 */
public class RateWidthLayout extends RelativeLayout {
    double rate=314d/414d;
    public RateWidthLayout(Context context, AttributeSet attrs){
        super(context,attrs);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        int parentWidth=MeasureSpec.getSize(widthSpec);
        Aide.t("parentWidth:"+parentWidth);
        switch (MeasureSpec.getMode(widthSpec)) {
            case MeasureSpec.EXACTLY:
                // Nothing to do
                Aide.t("1");
                break;
            case MeasureSpec.AT_MOST:
                Aide.t("2");
                widthSpec = MeasureSpec.makeMeasureSpec(
                        Math.min(parentWidth, (int)(parentWidth*rate)), MeasureSpec.EXACTLY);
                Aide.t("measured:"+MeasureSpec.getSize(widthSpec));
                break;
            case MeasureSpec.UNSPECIFIED:
                Aide.t("3");
                widthSpec = MeasureSpec.makeMeasureSpec( (int)(parentWidth*rate), MeasureSpec.EXACTLY);
                break;
        }
        // Let super sort out the height
        super.onMeasure(widthSpec, heightSpec);
    }
}
